package com.masterclient.app.modules.cart.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.cart.beans.GatherBean;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/6/23 16:32
 * 功能说明：
 */

public class GatherGoodsRecyclerAdapter extends BaseQuickAdapter<GatherBean.DataBean.ProductsBean, BaseViewHolder> {

    private int width;

    public GatherGoodsRecyclerAdapter() {
        super(R.layout.item_footer_good);
        width = (int) ((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(41)) / 2f);
    }

    @Override
    protected void convert(BaseViewHolder helper, GatherBean.DataBean.ProductsBean item) {

        renderAllPrice(helper,item);

        TextView tvFavoriteNum =  helper.getView(R.id.tv_favorite_num);
        tvFavoriteNum.setVisibility(View.GONE);

        ImageView imageView =  helper.getView(R.id.iv_goods);
        ViewTransformUtil.glideImageView(mContext,item.image,imageView,new RoundedCornersTransformation(0, 0),
                R.mipmap.defaults,width,width);

        helper.setText(R.id.tv_sub_title,item.title);

        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);

        if("1".equals(item.isNew)){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }


        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewCommodityDetailActivity.class);
                intent.putExtra("id",item.productId);
                intent.putExtra("source_position",helper.getAdapterPosition()-getHeaderLayoutCount()+"");
                mContext.startActivity(intent);
            }
        });
    }


    private void renderAllPrice(BaseViewHolder helper, GatherBean.DataBean.ProductsBean item) {
        TextView tvPrice = helper.getView(R.id.price);
        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);

        Utils.set_price_style2(tvPrice,"¥ " + item.price);
        Utils.setDin(tvPrice,mContext);
        tvOfficialPrice.setText("¥" +item.linePrice);
        tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        tvOfficialPrice.setVisibility(View.VISIBLE);
    }
}
